﻿#ifdef _WINDOWS
#include <windows.h>
#endif
#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>
#include "OpenGL.h"
#include "ScreenManager.h"
#include "MenuScreen.h"
#include "SoundManager.h"

#pragma warning(disable:4305)//disable implicit float-double casting

ScreenManager *screenManager;

OpenGL::OpenGL()
{
}

OpenGL::~OpenGL()
{
}

bool OpenGL::Init()
{   
	//khi khoi tao Screen chi khoi tao 1 Menu Screen duy nhat
	screenManager = new ScreenManager();
	MenuScreen* mainMenu = new MenuScreen(screenManager);
	screenManager->AddScreen(mainMenu);
	glClearColor(0.0, 0.0, 0.0, 0.0);
    return true;
}

bool OpenGL::Shutdown()
{
    return true;
}

void OpenGL::SetupProjection(int width, int height)
{
    if (height == 0)                    // don't want a divide by zero
    {
        height = 1;                 
    }

    glViewport(0, 0, width, height);        // reset the viewport to new dimensions
    glMatrixMode(GL_PROJECTION);            // set projection matrix current matrix
    glLoadIdentity();                       // reset projection matrix

    // calculate aspect ratio of window
    gluPerspective(52.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);
    glMatrixMode(GL_MODELVIEW);             // set modelview matrix
    glLoadIdentity();                       // reset modelview matrix

    m_windowWidth = width;
    m_windowHeight = height;

	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
	glClearColor(1.0f, 1.0f, 1.0f, 0.5f);				// Black Background
	glClearDepth(1.0f);									// Depth Buffer Setup
	glDepthMask(true);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations

}

void OpenGL::Prepare(float _dt)
{
	screenManager->MouseLoad();
	screenManager->KeyInput();
	screenManager->Update(_dt);
}

void OpenGL::Render()
{
	// clear screen and depth buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);    
    glLoadIdentity();
    screenManager->DrawScreen();
}

